<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class GoodsCategoryRelation extends Model
{
    /**
     * 关联的数据表
     */
    protected $table = 'ims_yz_goods_category';

    /**
     * 可以批量赋值的属性
     */
    protected $fillable = [
        'goods_id',
        'category_id',
        'category_ids',
        'goods_option_id',
        'created_at',
        'updated_at',
        'deleted_at'
    ];

    /**
     * 自动转换的属性
     */
    protected $casts = [
        'goods_id' => 'integer',
        'category_id' => 'integer',
        'goods_option_id' => 'integer',
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
        'deleted_at' => 'datetime'
    ];

    /**
     * 关联商品
     */
    public function goods()
    {
        return $this->belongsTo(Goods::class, 'goods_id', 'id');
    }

    /**
     * 关联商品分类
     */
    public function category()
    {
        return $this->belongsTo(GoodsCategory::class, 'category_id', 'id');
    }

    /**
     * 根据商品ID查询分类关联
     */
    public static function findByGoodsId($goodsId)
    {
        return static::where('goods_id', $goodsId)->get();
    }

    /**
     * 根据分类ID查询商品关联
     */
    public static function findByCategoryId($categoryId)
    {
        return static::where('category_id', $categoryId)->get();
    }

    /**
     * 根据商品ID列表查询分类关联
     */
    public static function findByGoodsIds($goodsIds)
    {
        return static::whereIn('goods_id', $goodsIds)->get();
    }
}
